package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.RandomStringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyBuyProduct;
import com.ruoyi.system.domain.WdyProduct;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyBuyProductService;
import com.ruoyi.system.service.IWdyProductService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 购买记录 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-12-17
 */
@Controller
@RequestMapping("/system/wdyBuyProduct")
public class WdyBuyProductController extends BaseController
{
    private String prefix = "system/wdyBuyProduct";
	
	@Autowired
	private IWdyBuyProductService wdyBuyProductService;
	@Autowired
	private IWdyUserService wdyUserService;
	@Autowired
	private IWdyProductService wdyProductService;
	@RequiresPermissions("system:wdyBuyProduct:view")
	@GetMapping()
	public String wdyBuyProduct()
	{
	    return prefix + "/wdyBuyProduct";
	}
	
	/**
	 * 查询购买记录列表
	 */
	@RequiresPermissions("system:wdyBuyProduct:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyBuyProduct wdyBuyProduct)
	{
		startPage();
        List<WdyBuyProduct> list = wdyBuyProductService.selectWdyBuyProductList(wdyBuyProduct);
		for(WdyBuyProduct e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出购买记录列表
	 */
	@RequiresPermissions("system:wdyBuyProduct:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyBuyProduct wdyBuyProduct)
    {
    	List<WdyBuyProduct> list = wdyBuyProductService.selectWdyBuyProductList(wdyBuyProduct);
        ExcelUtil<WdyBuyProduct> util = new ExcelUtil<WdyBuyProduct>(WdyBuyProduct.class);
        return util.exportExcel(list, "wdyBuyProduct");
    }
	
	/**
	 * 新增购买记录
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		WdyProduct product = wdyProductService.getById(1);
		mmap.put("product", product);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存购买记录
	 */
	@RequiresPermissions("system:wdyBuyProduct:add")
	@Log(title = "购买记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyBuyProduct wdyBuyProduct)
	{
		try {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_name", wdyBuyProduct.getUserName());
			WdyUser user = wdyUserService.getOne(query);
			if(user == null) {
				throw new BusinessException(ResultConstants.FI1000, "用户不存在");
			}
			wdyBuyProduct.setUserId(user.getUserId());
			wdyBuyProduct.setActiveCode(RandomStringUtils.getRandomString(6));
			wdyBuyProduct.setReleaseBase(wdyBuyProduct.getReleaseAmount());
			wdyBuyProduct.setCreateBy(ShiroUtils.getLoginName());
			wdyBuyProductService.buy(wdyBuyProduct);
		} catch (Exception e) {
			log.error("添加异常", e);
			return error();
		}
		return success();
	}

	/**
	 * 修改购买记录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyBuyProduct wdyBuyProduct = wdyBuyProductService.selectWdyBuyProductById(id);
		mmap.put("wdyBuyProduct", wdyBuyProduct);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存购买记录
	 */
	@RequiresPermissions("system:wdyBuyProduct:edit")
	@Log(title = "购买记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyBuyProduct wdyBuyProduct)
	{		
		return toAjax(wdyBuyProductService.updateWdyBuyProduct(wdyBuyProduct));
	}
	
	/**
	 * 删除购买记录
	 */
	@RequiresPermissions("system:wdyBuyProduct:remove")
	@Log(title = "购买记录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyBuyProductService.deleteWdyBuyProductByIds(ids));
	}


	@RequiresPermissions("system:wdyBuyProduct:view")
	@PostMapping("/getSum")
	@ResponseBody
	public AjaxResult getSum(WdyBuyProduct wdyBuyProduct)
	{
		AjaxResult success = success();
		Map<String, BigDecimal> sum = wdyBuyProductService.getSum(wdyBuyProduct);
		return success.put("sum", sum);
	}


}
